package com.hahaliu.hahaliu.controller.user;

import com.hahaliu.hahaliu.common.result.Result;
import com.hahaliu.hahaliu.common.context.CurrentUser;
import com.hahaliu.hahaliu.pojo.entity.Favorite;
import com.hahaliu.hahaliu.service.video.FavoriteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/user/favorite")
@Api(tags = "用户端收藏夹相关接口")
@Slf4j
public class FavoriteController {

	@Autowired
	private FavoriteService favoriteService;
	@Autowired
	private CurrentUser currentUser;

	@GetMapping("/list")
	@ApiOperation("获取用户收藏夹")
	public Result<List<Favorite>> getFavoriteList(@RequestParam("uid") Integer uid) {
		Integer currentId = currentUser.getUserId();
		List<Favorite> favoriteList = null;
		if (currentId.equals(uid)) {
			favoriteList = favoriteService.getFavoriteList(uid, true);
		} else {
			favoriteList = favoriteService.getFavoriteList(uid, false);
		}
		return Result.success(favoriteList);
	}

	/**
	 * 站内用户请求某个用户的收藏夹列表（需要jwt鉴权）
	 * @param uid   被查看的用户ID
	 * @return  包含收藏夹列表的响应对象
	 */
	@GetMapping("/get-all/user")
	public Result<List<Favorite>> getAllFavoritesForUser(@RequestParam("uid") Integer uid) {
		Integer loginUid = currentUser.getUserId();
		if (Objects.equals(loginUid, uid)) {
			return Result.success(favoriteService.getFavoriteList(uid, true));
		} else {
			return Result.success(favoriteService.getFavoriteList(uid, false));
		}
	}

	/**
	 * 游客请求某个用户的收藏夹列表（不需要jwt鉴权）
	 * @param uid   被查看的用户ID
	 * @return  包含收藏夹列表的响应对象
	 */
	@GetMapping("/favorite/get-all/visitor")
	public Result<List<Favorite>> getAllFavoritesForVisitor(@RequestParam("uid") Integer uid) {
		return Result.success(favoriteService.getFavoriteList(uid, false));
	}

	@PostMapping("/create")
	@ApiOperation("创建一个新的收藏夹")
	public Result<Favorite> createFavorite(@RequestParam("title") String title,
	                                     @RequestParam("desc") String desc,
	                                     @RequestParam("visible") Integer visible) {
		Integer currentId = currentUser.getUserId();
		return Result.success(favoriteService.addFavorite(currentId, title, desc, visible));
	}

	@PostMapping("/cancel")
	@ApiOperation("取消收藏")
	public Result createFavorite(@RequestParam("vid") Integer vid, @RequestParam("fid") Integer fid) {
		favoriteService.cancelFavorite(vid, fid);
		return Result.success();
	}
}
